/**
 * 
 */
package denaris.core.entity;

import javax.microedition.lcdui.game.Sprite;

import denaris.core.AbstractDenarisMap;
import denaris.core.controller.AbstractController;
import denaris.factory.ImageFactory;
import denaris.midlet.DenarisGameCanvas;
import denaris.midlet.GameScreen;

/**
 * @author stkl23
 *
 */
public class Collectible extends AbstractEntity {
	private class CollectibleController extends AbstractController{
		public boolean advance() {
			boolean result = true;
			int dy = DenarisGameCanvas.CURRENT_FRAME % 3 == 0 ? -1 : 0;
			controllable.move(-GameScreen.SCROLL_SPEED, dy);
			if (controllable.getX() < -20 || controllable.getY() < -20) {
				result = false;
			}
			return result;
		}
	}

	public static final int COLLECTIBLE_SHOT = 0;
	public static final int COLLECTIBLE_MISSILES = 1;
	public static final int COLLECTIBLE_SHIELD = 2;
	public static final int COLLECTIBLE_NUKE = 3;
	public static final int COLLECTIBLE_SCORE = 4;
	public static final int[] SCORE  = {400, 400, 400, 750, 2000};
	
	/** indicates the type of the Collectible (see the COLLECTIBLE constants)*/
	private int type;

	public Collectible(AbstractDenarisMap parentMap, int x, int y, int type) {
		this(parentMap, x, y, type, true);
	}
	
	public Collectible(AbstractDenarisMap parentMap, int x, int y, int type, boolean changeSprite) {
		super(parentMap, SCORE[type]);
		setType(type);
		// create the sprite
		AbstractController collectibleController = getController();
		Sprite sprite = collectibleController.getSprite(); 
		if (changeSprite) {
			sprite = new Sprite(ImageFactory.getImage("/denaris/resource/sprite/spheres.png"), 14, 14);
			collectibleController.setSprite(sprite);
			sprite.setFrame(type);
		}
		sprite.setPosition(x, y);
	}
	
	public void onScopeLeft() {
		parentMap.removeFriend(this);
	}

	public AbstractController createController() {
		return new CollectibleController();
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}
	
	public Explosion explode(int childExplosions, int frameDelay) {
		Explosion expl = super.explode(childExplosions, frameDelay);
		parentMap.addFriend(expl);
		parentMap.removeFriend(this);
		
		return expl;
	}

}
